﻿using System.Collections.ObjectModel;
using System.Net;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Web.Http;
using Microsoft.Web.WebPages.OAuth;
using Monitor.Filters;
using Monitor.Models.JsonModel;
using Monitor.Repository.UserRepository.LinqModel;
using ServiceStack.Text;


namespace Monitor.Controllers.ApiControllers
{
    public class AreasController : MainApiController
    {

        #region извлекает дерево зон
        [WebApiOutputCache(2,300)]
        public Collection<TreeElementJson> GetAreas()
        {
            Collection<TreeElementJson> result = new Collection<TreeElementJson>();
            try
            {
                IEnumerable<AreaGroup> areaGroups = CurrentUser.AreaGroups;
                foreach (AreaGroup areaGroup in areaGroups)
                {
                    TreeElementJson groupJson = new TreeElementJson()
                    {
                        Id = "tree_" + areaGroup.GroupAreaId.ToString(),
                        ParentId = (areaGroup.ParentId == -1) ? "-1" : "tree_" + areaGroup.ParentId.ToString(),
                        Class = "group",
                        Name = areaGroup.FullName
                    };
                    result.Add(groupJson);
                    foreach (Area area in areaGroup.Areas)
                    {
                        TreeElementJson item = new TreeElementJson()
                        {
                            Id = area.AreaId.ToString(),
                            ParentId = groupJson.Id,
                            Class = "car_7540",
                            Name = area.FullName
                        };
                        result.Add(item);
                    }
                }

            }
            catch (Exception exception)
            {
                ErrorLogger.Error(this.ControllerContext,exception);
            }
            return result;
        }
        #endregion

        #region поиск среди доступных зон по имени
        [WebApiOutputCache(5, 60, false)]
        public IEnumerable<TreeElementJson> Get(string name)
        {
            try
            {
                return CurrentUser.Areas.Where(n => n.FullName.Contains(name)).Select(n => new TreeElementJson()
                                                                                               {
                                                                                                   Id =
                                                                                                       n.AreaId.ToString
                                                                                                       (),
                                                                                                   Class = "car_7540",
                                                                                                   Name = n.FullName
                                                                                               });
            }
            catch (Exception ex)
            {
                ErrorLogger.Error(this.ControllerContext,ex);
            }
            return null;
        }

        #endregion

        #region извлекает информацию по одной конкретной зоне
        [WebApiOutputCache()]
        public AreaInfo GetArea(Int64 id)
        {
            try
            {
                Area area = CurrentUser.Areas.FirstOrDefault(n=>n.AreaId==id);
                if (area != null)
                {
                    return new AreaInfo(area);
                }
            }
            catch(Exception ex)
            {
                ErrorLogger.Error(this.ControllerContext,ex);
            }
            return null;
        }

        #endregion
        
    }
}